//
//  XD_NotificationListTabCell.swift
//  XueDao
//
//  Created by wenwen on 2021/4/15.
//

import UIKit

protocol XD_NotificationListTabCellDelegate {
    func showOrHiddenContents(cell: XD_NotificationListTabCell)
}

let contentFont: UIFont = .systemFont(ofSize: 14, weight: .regular)
let contentColor: UIColor = UIColor(red: 0.45, green: 0.47, blue: 0.49, alpha: 1.00)
let suffixColor: UIColor = UIColor(red: 0.16, green: 0.74, blue: 0.61, alpha: 1.00)

class XD_NotificationListTabCell: UITableViewCell {
    
    var _model : XS_NotificationListModel!
    public var model : XS_NotificationListModel!{
        set{
            _model = newValue
            //设置数据
            bageV.isHidden = newValue.state != "notread"
            titleLab.text = newValue.type == "ordinaryMsg" ? "官方消息" : "其他消息"
            timeLab.text = newValue.createTime

            contentLab.text = newValue.content
            contentOpenOrCloseState(dModel: newValue)
        }
        get{
            return _model
        }
    }
    
    var cellDelegate: XD_NotificationListTabCellDelegate?
    
    lazy var iconIv: UIImageView = {
        let iconIv = UIImageView()
        iconIv.layer.masksToBounds = true
        iconIv.layer.cornerRadius = 20
        iconIv.image = UIImage.init(named: "编组 8")
        return iconIv
    }()
    
    lazy var bageV: UIImageView = {
        let bageV = UIImageView()
        bageV.backgroundColor = UIColor(red: 0.93, green: 0.40, blue: 0.25, alpha: 1.00)
        bageV.layer.masksToBounds = true
        bageV.layer.cornerRadius = 4
        return bageV
    }()
    
    lazy var titleLab: UILabel = {
        let titleLab = UILabel()
        titleLab.textAlignment = .left
        titleLab.font = .systemFont(ofSize: 14, weight: .medium)
        titleLab.textColor = UIColor(red: 0.22, green: 0.24, blue: 0.25, alpha: 1.00)
        return titleLab
    }()
    
    lazy var timeLab: UILabel = {
        let timeLab = UILabel()
        timeLab.textAlignment = .right
        timeLab.font = .systemFont(ofSize: 12, weight: .regular)
        timeLab.textColor = UIColor(red: 0.67, green: 0.69, blue: 0.72, alpha: 1.00)
        return timeLab
    }()
    
    lazy var contentLab: UILabel = {
        let contentLab = UILabel()
        contentLab.contentMode = .topLeft
        contentLab.numberOfLines = 2
        contentLab.font = contentFont
        contentLab.textColor = contentColor
        return contentLab
    }()
    
    lazy var openOrCloseBtn: UIButton = {
       let openOrCloseBtn = UIButton()
        openOrCloseBtn.setTitle("展开", for: .normal)
        openOrCloseBtn.setImage(UIImage.init(named: "openContent"), for: .normal)
        openOrCloseBtn.titleLabel?.font = contentFont
        openOrCloseBtn.setTitleColor(UIColor(red: 0.16, green: 0.74, blue: 0.61, alpha: 1.00), for: .normal)
        openOrCloseBtn.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: -9, bottom: 0, right: 9)
        openOrCloseBtn.imageEdgeInsets = UIEdgeInsets.init(top: 0, left: 29 + 2, bottom: 0, right: -29 - 2)
        openOrCloseBtn.addTarget(self, action: #selector(openOrCloseContent(btn:)), for: .touchUpInside)
        return openOrCloseBtn
    }()
    
    lazy var lineView: UIView = {
        let lineView = UIView()
        lineView.backgroundColor = UIColor.gray.withAlphaComponent(0.1)
        return lineView
    }()
    
    private func setUpUI() {
        contentView.addSubview(iconIv)
        iconIv.snp.makeConstraints { (make) in
            make.top.equalTo(20)
            make.left.equalTo(16)
            make.width.height.equalTo(40)
        }
        
        contentView.addSubview(bageV)
        bageV.snp.makeConstraints { (make) in
            make.top.equalTo(iconIv.snp.top)
            make.right.equalTo(iconIv.snp.right).offset(-2)
            make.width.height.equalTo(8)
        }
        
        contentView.addSubview(timeLab)
        timeLab.snp.makeConstraints { (make) in
            make.top.equalTo(iconIv.snp.top)
            make.right.equalTo(-16)
            make.height.equalTo(16)
        }
        
        contentView.addSubview(titleLab)
        titleLab.snp.makeConstraints { (make) in
            make.top.equalTo(iconIv.snp.top)
            make.left.equalTo(iconIv.snp.right).offset(12)
            make.right.equalTo(timeLab.snp.left).offset(-12)
            make.height.equalTo(16)
        }
        
        contentView.addSubview(contentLab)
        contentLab.snp.makeConstraints { (make) in
            make.top.equalTo(titleLab.snp.bottom).offset(8)
            make.left.equalTo(iconIv.snp.right).offset(12)
            make.right.equalTo(-16)
        }

        contentView.addSubview(openOrCloseBtn)
        openOrCloseBtn.snp.makeConstraints { (make) in
            make.top.equalTo(contentLab.snp.bottom).offset(4)
            make.left.equalTo(contentLab)
            make.width.equalTo(40)
            make.height.equalTo(20)
            make.bottom.equalTo(-16)
        }
        
        contentView.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(1)
        }
    }
    
    @objc func openOrCloseContent(btn: UIButton) {
        if cellDelegate != nil {
            cellDelegate?.showOrHiddenContents(cell: self)
        }
    }
    
    class func getCellHeight(dModel: XS_NotificationListModel) -> CGFloat {
        let contentStr = dModel.content as NSString
        let numOfLines = contentHeight(text: contentStr) / contentFont.lineHeight
        
        let otherHeight: CGFloat = (20 + 16 + 8 + 16 + 1) + (numOfLines > 2 ? 24 : 0)
        
        let height = otherHeight + (dModel.isOpened == false && numOfLines > 2 ? 2.0 * contentFont.lineHeight : contentHeight(text: contentStr))

        return height + 1 // 加1向上取整
    }

    class func contentHeight(text: NSString) -> CGFloat {
//        let otherHeight: CGFloat = (20 + 16 + 8 + 16 + 20 + 1)
        let width = UIScreen.main.bounds.size.width - (16 + 40 + 12 + 16)
        return text.boundingRect(with: CGSize.init(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [.font : contentFont], context: nil).height
    }
    
    func contentOpenOrCloseState(dModel: XS_NotificationListModel) {
        contentLab.numberOfLines = dModel.isOpened ? 0 : 2
        openOrCloseBtn.setTitle(dModel.isOpened ? "收回" : "展开", for: .normal)
        openOrCloseBtn.setImage(UIImage.init(named: dModel.isOpened ? "closeContent" : "openContent"), for: .normal)
        let numOfLines = XD_NotificationListTabCell.contentHeight(text: dModel.content as NSString) / contentFont.lineHeight
        if numOfLines > 2 {
            openOrCloseBtn.isHidden = false
            openOrCloseBtn.snp.updateConstraints { (make) in
                make.top.equalTo(contentLab.snp.bottom).offset(4)
                make.height.equalTo(20)
            }
        } else {
            openOrCloseBtn.isHidden = true
            openOrCloseBtn.snp.updateConstraints { (make) in
                make.top.equalTo(contentLab.snp.bottom).offset(0)
                make.height.equalTo(0)
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        separatorInset = UIEdgeInsets.init(top: 0, left: 16, bottom: 0, right: 16)
//        layoutMargins = .zero
//        preservesSuperviewLayoutMargins = false
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
